package com.greenline.push.service.core.utils;

import java.util.Date;
import com.alibaba.fastjson.JSON;
import com.greenline.push.dal.pushcenter.dataobject.MsgForSendDO;
import com.greenline.push.service.core.item.Packet;

/**
 * convert packet to DB Object and convert DB object to packet
 * 
 * @author zhangdp
 * 
 */
public class PacketConvertor {

    /**
     * convert packet to scheduleDO
     * 
     * @param packet
     * @return
     */
    public MsgForSendDO convertPacketToForSendDO(Packet packet) {
        MsgForSendDO msgForSendDO = new MsgForSendDO();
        Long sendingTime = packet.getSendingTime();
        if (sendingTime != null) {
            msgForSendDO.setSendTime(new Date(sendingTime));
        }
        msgForSendDO.setContent(JSON.toJSONString(packet));//desearzable
        msgForSendDO.setType(packet.getType());// search use it
        return msgForSendDO;
    }

    /**
     * convert scheduleDO to packet
     * 
     * @param msgForSendDO
     * @return
     */
    public Packet convertForSendDOToPacket(MsgForSendDO msgForSendDO) {
        String content = msgForSendDO.getContent();
        Date d = msgForSendDO.getSendTime();
        Packet packet = JSON.parseObject(content, Packet.class);
        Long msgId = msgForSendDO.getId();
        packet.setDbMsgId(msgId);
        packet.setSendingTime(d == null ? null : d.getTime());
        return packet;
    }
}
